package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class StudentServiceImpl extends BaseService implements IStudentService {
    @Override
    public Integer add(StudentBean student) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student);
        session.commit();
        session.close();
        return student.getId();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer id, String phone) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id,phone);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findAll() {
        SqlSession session = this.getSession();
        IStudentMapper mapper =  session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper =  session.getMapper(IStudentMapper.class);
        StudentBean student = mapper.findById(id);
        session.close();
        return student;
    }

    @Override
    public List<StudentBean> findByName(String name) {

        SqlSession session = this.getSession();
        IStudentMapper mapper =  session.getMapper(IStudentMapper.class);
        List<StudentBean> list =  mapper.findByName(name);
        session.close();

        return list;
    }

    @Override
    public List<StudentBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IStudentMapper mapper =  session.getMapper(IStudentMapper.class);
        List<StudentBean> list =  mapper.findByBirthday(startDate,endDate);
        session.close();

        return list;
    }

    @Override
    public List<StudentBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IStudentMapper mapper =  session.getMapper(IStudentMapper.class);
        List<StudentBean> list =  mapper.findByItem(name,startDate,endDate);
        session.close();

        return list;
    }

    @Override
    public CutPageBean<StudentBean> cutAll(int pageNO) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        //封装当前页的数据
        cutBean.setList(mapper.cutByList(
                (pageNO - 1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        //封装总记录数
        cutBean.setCount(mapper.getTotal());

        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<StudentBean> cutByItem(int pageNO, String name, LocalDate startDate, LocalDate endDate) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<StudentBean>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);

        cutBean.setList(mapper.cutByItemList(
                (pageNO - 1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE,
                name,startDate,endDate));

        cutBean.setCount(mapper.cutByItemCount(name,startDate,endDate));
        session.close();
        return cutBean;
    }

    @Override
    public void addList(List<StudentBean> list) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.addList(list);
        session.commit();
        session.close();
    }

    @Override
    public void delArray(int[] idArray) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delArray(idArray);
        session.commit();
        session.close();
    }
}
